{% extends 'exam_system/base.html' %}

{% block title %}通知列表{% endblock %}

{% block content %}
<div class="container mt-4">
    <h2>我的通知</h2>
    <div class="notifications-list">
        {% if notifications %}
            {% for notification in notifications %}
                <div class="card mb-3 notification-item" data-id="{{ notification.id }}">
                    <div class="card-header d-flex justify-content-between align-items-center">
                        <h5 class="mb-0">{{ notification.title }}</h5>
                        <small class="text-muted">{{ notification.created_at|date:"Y-m-d H:i" }}</small>
                    </div>
                    <div class="card-body">
                        <p class="card-text">{{ notification.content|linebreaks }}</p>
                        {% if not notification.is_read %}
                            <button class="btn btn-sm btn-primary mark-as-read"
                                    data-notification-id="{{ notification.id }}">
                                标记为已读
                            </button>
                        {% endif %}
                    </div>
                </div>
            {% endfor %}
        {% else %}
            <div class="alert alert-info">
                暂无未读通知
            </div>
        {% endif %}
    </div>
</div>

{% block extra_js %}
<script>
$(document).ready(function() {
    // 标记通知为已读
    $('.mark-as-read').click(function() {
        var button = $(this);
        var notificationId = button.data('notification-id');

        $.ajax({
            url: '/exam/notification/' + notificationId + '/mark-read/',
            method: 'POST',
            headers: {
                'X-CSRFToken': $('input[name=csrfmiddlewaretoken]').val()
            },
            success: function(response) {
                if (response.status === 'success') {
                    // 移除按钮并添加已读标记
                    button.fadeOut(function() {
                        $(this).remove();
                    });
                }
            },
            error: function(xhr) {
                alert('操作失败，请重试');
            }
        });
    });
});
</script>
{% endblock %}
{% endblock %}